package com.pizza.server.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import org.hibernate.Session;

import com.mcol.common.rmi.Rejestruj;
import com.pizza.hibernate.Adres;
import com.pizza.hibernate.SessionFactoryUtil;
import com.pizza.hibernate.User;

public class RejestrujServer extends UnicastRemoteObject implements Rejestruj
{
Session session;

	public RejestrujServer() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
		 session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.getTransaction().commit();
	}

	@Override
	public User zarejestruj(
		
			String login,
			String pas1, 
			String pas2, 
			String email)
			throws RemoteException {
		
		try{
		 session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
        User user = new User();
		user.setEmail(email);
		user.setLogin(login);
		user.setPassword1(pas1);
		
		session.save(user);
		session.getTransaction().commit();
		System.out.println(user.getId());
		return  user;
	}catch(Exception ee){
		ee.printStackTrace();
		return null;
		}
	
	}



	@Override
	public boolean sprawdzLogin(String login) throws RemoteException {
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        ArrayList<User> a=(ArrayList<User>) session.createQuery("from User").list();
        session.getTransaction().commit();
        
        for (User user : a) 
        	if(login.equals(user.getLogin()))
        		return false;
        
        return true;
	}

	@Override
	public Adres rejestruj2(String name, String lastname, String numerTelefonu,
			String city, String ulica, int zip_code, int zip_code1,
			int numerDomu, boolean acceptShareDate, int idUser)
			throws RemoteException {
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Adres u = new Adres();
        u.setCity(city);
        u.setLastname(lastname);
        u.setName(name);
        u.setNumerDomu(numerDomu);
        u.setNumerTelefonu(numerTelefonu);
        u.setZip_code1(zip_code1);
        u.setZipcode(zip_code);
        u.setUlica(ulica);
        u.setIdUser(idUser);
        u.setAcceptShareDate(acceptShareDate);
		session.save(u);
        session.getTransaction().commit();
        return u;
	}

	@Override
	public void getId(String login) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	
}